package au.com.handsunited.web.controller.project;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.Project;
import au.com.handsunited.model.User;
import au.com.handsunited.service.EntityService;
import au.com.handsunited.service.ProjectService;
import au.com.handsunited.web.session.SessionSupport;

/**
 * @author arunda
 *
 */
public class JoinProjectController extends AbstractController {


	private ProjectService projectService;
	private EntityService entityService;
	private String successView;

	private static Logger logger = LoggerFactory.getLogger(JoinProjectController.class);



	/**
	 *
	 */
	public JoinProjectController() {

	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String projectId = request.getParameter("id");
		Project project = projectService.getProject(projectId);
		User currentUser = SessionSupport.getUser(request);
		projectService.addMemberToProject(project, currentUser);
		ModelAndView mav = new ModelAndView(successView, "entity", project);
		return mav;
	}

	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

}
